package middlewave

import (
	"backend/constants/errorCode"
	"github.com/beego/beego/v2/core/logs"
	"github.com/beego/beego/v2/server/web/context"
)

// CorsHandler
// 跨域处理
func CorsHandler(ctx *context.Context) {
	//origin, _ := beego.AppConfig.String("allowOrigin")
	method := ctx.Request.Method
	origin := ctx.Request.Header.Get("Origin")
	ctx.Output.Header("Access-Control-Allow-Origin",origin)
	ctx.Output.Header("Access-Control-Allow-Credentials", "true")
	ctx.Output.Header("Access-Control-Allow-Headers","Origin, Authorization, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type, Token, Cookie")
	ctx.Output.Header("Access-Control-Allow-Methods","POST, GET, OPTIONS")
	if method == "OPTIONS"{
		ctx.Output.SetStatus(200)
		result := errorCode.OK
		err := ctx.Output.JSON(result,true,true)
		if err != nil {
			logs.Error(err.Error())
		}
		return
	}
}